/**
 * Copyright &copy; com.smooth.common
 */
package com.smooth.site.sx.mag.biz.link.service;

import java.util.List;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.smooth.common.site.common.PageDb;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.smooth.dao.gen.bean.BizLink;
import com.smooth.dao.gen.bean.BizLinkExample;
import com.smooth.dao.gen.BizLinkMapper;
import com.smooth.common.site.common.enums.DelFlagEnums;
import com.smooth.common.site.common.persistence.Page;
import com.smooth.common.site.common.utils.IdGenerater;

/**
 * 友情链接管理Service
 * @author bm
 * @version 2019-03-09
 */
@Service
@Transactional(readOnly = true)
public class BizLinkService{
	Logger log = LoggerFactory.getLogger(BizLinkService.class);
	@Autowired
	BizLinkMapper bizLinkMapper;
	@Autowired
	IdGenerater idGenerater;
	
	public BizLink get(String no) {
		return bizLinkMapper.selectByPrimaryKey(no);
	}
	
	public List<BizLink> findList(BizLink bizLink) {
		BizLinkExample bizLinkExample = new BizLinkExample();
		BizLinkExample.Criteria criteria = bizLinkExample.createCriteria();
		
		return bizLinkMapper.selectByExample(bizLinkExample);
	}
	
	public Page<BizLink> findPage(Page<BizLink> page, BizLink bizLink) {
		BizLinkExample bizLinkExample = new BizLinkExample();
		BizLinkExample.Criteria criteria = bizLinkExample.createCriteria();
		
		bizLinkExample.setPage(page);
		
		List<BizLink> dlist = bizLinkMapper.selectByExample(bizLinkExample);
		page.setList(dlist);
		page.setCount(bizLinkMapper.countByExample(bizLinkExample));
		
		return page;
	}
	
	@Transactional(readOnly = false)
	public BizLink save(BizLink bizLink) {
		if(StringUtils.isNotEmpty(bizLink.getId())&&get(bizLink.getId())!=null) {
			int optNum = bizLinkMapper.updateByPrimaryKeySelective(bizLink);
			if(optNum==1) {
				log.info("更新 student 数据【"+bizLink.toString()+"】成功");
			}else {
				log.error("更新 student 数据【"+bizLink.toString()+"】失败");
			}
			return bizLinkMapper.selectByPrimaryKey(bizLink.getId());
		}else {
			Date date = new Date();
			bizLink.setCreateDate(date);
			bizLink.setId(idGenerater.genClsNo("bizLink"));
			bizLink.setUpdateDate(date);
			int optNum =  bizLinkMapper.insertSelective(bizLink);
			if(optNum==1) {
				log.info("插入 student 数据【"+bizLink.toString()+"】成功");
			}else {
				log.error("插入 student 数据【"+bizLink.toString()+"】失败");
			}
			return bizLink;
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(BizLink bizLink) {
		bizLinkMapper.deleteByPrimaryKey(bizLink.getId());
	}
	
}